{% extends "base.html" %}
{% block title %}<title>{{ cur.0.module_name }}-消防系统计算平台</title>{% endblock %}
{% block extrastyle %}
    {% load static %}
    <link rel="stylesheet" href="{% static 'css/jsuites.css' %}" type="text/css"/>
    <link rel="stylesheet" href="{% static 'css/jexcel.css' %}" type="text/css"/>
    <script src="{% static 'js/jexcel.js' %}"></script>
    <script src="{% static 'js/jsuites.js' %}"></script>
{% endblock %}

{% block header %}
    {% include "header.html" %}
    {% include "select_module.html" %}
{% endblock %}
{% block content %}
    <div class="jq22-demo center">
        {% if tables %}
            <div>
                {% for var in tables %}
                    <div><a style="color: #000;" href="{% url 'excel' var.id %}">{{ var.table.table_name }}</a>
                        | {{ var.table.catalog }} | {{ var.table.subtitle1 }} | {{ var.table.subtitle2 }} </div>
                {% endfor %}
            </div>
        {% else %}

        {% endif %}


    </div>
{% endblock %}
{% block nav %}
    {{ block.super }}
    <div class="pager">
        {% if pre %}
            <a href="{% url 'module' p.id pre.id %}"><button class="btn-primary">上一步</button></a>
        {% endif %}
        {% if next %}
            <a href="{% url 'module' p.id next.id %}"><button class="btn-primary">下一步</button></a>
        {% endif %}
    </div>
{% endblock %}
{% block extrajs %}
    {{ block.super }}
    <script>
        {% for table in tables %}
            //提交保存
            function submit{{ table.id }}() {
                table{{ table.id }}.formula = []
                txt = JSON.stringify(table{{ table.id }}.getDataAndFormula(false))
                csrf = $("input[name='csrfmiddlewaretoken']").val()
                $.post("{% url 'excel' table.pid %}", {
                    data: txt,
                    csrfmiddlewaretoken: csrf
                }, function (result) {
                    if (result.code == undefined) {
                        window.location.href = '/login/'
                    } else {
                        $.globalMessenger().post({
                            message: result.data,
                            hideAfter: 6,
                            type: result.msg,
                            //showCloseButton: true
                        });
                    }
                })
            }
            {#气体系统 根据第一张表的内容自动填充后面的表#}
            {% if table.id == 29 %}
                rows = {'196':1, '197': 1, '198': 1} // 系统名称对应的表中的行
                var olda=[];
                var beforeChange{{ table.id }} = function(instance, cell, x, y, value) {
                    var row =parseInt(y)+1;
                    if (!(row in olda)) {
                        olda[row] = {'A': '', 'B': ''}
                    }
                    if (x == 0) {//改了“系统名称”
                        if ('B' in olda[row] && olda[row]['B'] != '') {
                            if (olda[row]['A'] == '' && value != '') {
                                //chgmode = 'new'
                                olda[row]['A'] = value
                                newRows(row) // 新增
                            } else if (olda[row]['A'] != '' && value != '' && value != olda[row]['A']) {
                                //chgmode = 'change'
                                rows[olda[row]['A']]
                                olda[row]['A'] = value
                                deleteRows(row) //删除之前数据
                                newRows(row) //新增新数据
                            }
                        } else {
                            olda[row]['A'] = value
                        }
                        checkUnique();
                    }
                    else if (x==1){//改了“防护区名称”
                        if ('A' in olda[row] && olda[row]['A'] != '') {
                            if (olda[row]['B'] == '' && value != '') {
                                //chgmode = 'new'
                                olda[row]['B'] = value
                                newRows(row) // 新增
                            } else if (olda[row]['B'] != '' && value != '' && value != olda[row]['B']) {
                                //chgmode = 'change'
                                olda[row]['B'] = value
                                changeRows(row) // 修改
                            }
                        } else {
                            olda[row]['B'] = value
                        }
                        checkUnique();
                    }
                }
                // 删除行之前触发
                var beforeDelete = function (el, rowNumber, numOfRows) {
                    var row = parseInt(rowNumber)+1;
                    if (row in olda) {
                        deleteRows(row)
                        olda.splice(row, 1)
                    }
                    return true
                }
                {#用名称字段匹配，给派生表赋字段值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if(x == 2 || x == 3 || x==5 || x==6){
                        let r1 = parseInt(y)+1
                        let type = table29.getValue('A'+ r1,true);
                        if(type == "七氟丙烷气体灭火系统"){
                            if(table30.getColumnData != undefined || table34.getColumnData != undefined){
                                var arr1 = table30.getColumnData(0)
                                var arr2 = table34.getColumnData(0)
                                let name = table29.getValue('B'+ r1,true);
                                {#七氟丙烷气体灭火系统计算表#}
                                for(let i=0;i<arr1.length;i++){
                                    if(arr1[i]==name){
                                        let rnum = parseInt(i)+1
                                        if(x==2){
                                            let consistence = table29.getValue('C'+ r1,true);
                                            table30.setValue('G'+ rnum,consistence);
                                        }
                                        else if(x==3){
                                            let time = table29.getValue('D'+ r1,true);
                                            table30.setValue('I'+ rnum,time);
                                        }
                                    }
                                }
                                {#七氟丙烷气体灭火钢瓶选型表#}
                                for(let i=0;i<arr2.length;i++){
                                    if(arr2[i]==name){
                                        let rnum = parseInt(i)+1
                                        if(x==2){
                                            let consistence = table29.getValue('C'+ r1,true);
                                            table34.setValue('D'+ rnum,consistence);
                                        }
                                        else if(x==5){
                                            let plevel = table29.getValue('F'+ r1,true);
                                            table34.setValue('E'+ rnum,plevel);
                                        }
                                        else if(x==6){
                                            let weight = table29.getValue('G'+ r1,true);
                                            table34.setValue('G'+ rnum,weight);
                                        }
                                    }
                                }
                            }
                        }
                        else if(type == "IG541混合气体灭火系统"){
                            if(table31.getColumnData != undefined || table32.getColumnData != undefined){
                                var arr1 = table31.getColumnData(0)
                                var arr2 = table32.getColumnData(0)
                                let name = table29.getValue('B'+ r1,true);
                                {#IG541混合气体灭火系统计算表#}
                                for(let i=0;i<arr1.length;i++){
                                    if(arr1[i]==name){
                                        let rnum = parseInt(i)+1
                                        if(x==2){
                                            let consistence = table29.getValue('C'+ r1,true);
                                            table31.setValue('G'+ rnum,consistence);
                                        }
                                        else if(x==3){
                                            let time = table29.getValue('D'+ r1,true);
                                            table31.setValue('I'+ rnum,time);
                                        }
                                    }
                                }
                                {#IG541混合气体灭火钢瓶选型表#}
                                for(let i=0;i<arr2.length;i++){
                                    if(arr2[i]==name){
                                        let rnum = parseInt(i)+1
                                        if(x==2){
                                            let consistence = table29.getValue('C'+ r1,true);
                                            table32.setValue('C'+ rnum,consistence);
                                        }
                                        else if(x==5){
                                            let plevel = table29.getValue('F'+ r1,true);
                                            table32.setValue('D'+ rnum,plevel);
                                        }
                                        else if(x==6){
                                            let weight = table29.getValue('G'+ r1,true);
                                            table32.setValue('F'+ rnum,weight);
                                        }
                                    }
                                }
                            }
                        }
                        else if(type == "热气溶胶预制灭火系统"){
                            if(table33.getColumnData != undefined){
                                var arr = table33.getColumnData(0)
                                let name = table29.getValue('B'+ r1,true);
                                for(let i=0;i<arr.length;i++){
                                    if(arr[i]==name){
                                        let consistence = table29.getValue('C'+ r1,true);
                                        let rnum = parseInt(i)+1
                                        table33.setValue('D'+ rnum,consistence);
                                    }
                                }
                            }
                        }
                    }
                }
            {% elif table.id == 3 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==9 && y==rows-1 && table4.setValue != undefined){
                        table4.setValue('D1',value);
                    }
                }
                {% elif table.id == 4 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if(x==8 && table5.setValue != undefined){
                        table5.setValue('D1',value);
                    }
                    if((x==6 || x==8) && table6.setValue != undefined ){
                        var p1=ROUND(table4.getValue('I1',true)*1.2,2);
                        var p2=ROUND(table4.getValue('G1',true),2);
                        var p = p1+p2;
                        p = ROUND(p,2)
                        table6.setValue('H1',p);
                    }
                }
                {% elif table.id == 6 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if((x==1 ||x==3) && table7.setValue != undefined){
                        var p1=ROUND(table6.getValue('B1',true)*100,2);
                        var p2=ROUND(table6.getValue('D1',true),2);
                        var p = p1-p2+5;
                        p = ROUND(p,2)
                        table7.setValue('D1',p);
                    }
                }
                {% elif table.id == 8 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==9 && y==rows-1 && table9.setValue != undefined){
                        table9.setValue('C1',value);
                    }

                }
                {% elif table.id == 10 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==9 && y==rows-1 && table11.setValue != undefined){
                        table11.setValue('D1',value);
                    }
                }
                {% elif table.id == 11 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if(x==8  && table12.setValue != undefined){
                        table12.setValue('D1',value);
                    }
                    {#室内消火栓系统“水泵扬程的计算-G、I”给“启泵压力计算-泄压阀开启压力”赋值#}
                    if((x==6 || x==8) && table13.setValue != undefined){
                        var p1=ROUND(table11.getValue('I1',true)*1.2,2);
                        var p2=ROUND(table11.getValue('G1',true),2);
                        var p = p1+p2;
                        p = ROUND(p,2)
                        table13.setValue('H1',p);
                    }
                }
                {% elif table.id == 13 %}
                {#室内系统“启泵压力计算-B、D”给“室内消火栓系统稳压泵计算-单台水泵扬程”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if((x==1 || x==3) && table14.setValue != undefined){
                        var p1=ROUND(table13.getValue('B1',true)*100,2);
                        var p2=ROUND(table13.getValue('D1',true),2);
                        var p = p1-p2+5;
                        p = ROUND(p,2)
                        table14.setValue('D1',p);
                    }
                }
                {% elif table.id == 15 or table.id == 21 %}
                {#室内灭火系统“减压孔板计算计算-栓口动压、栓口静压”计算#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    {#栓口动压#}
                    r= parseInt(y)+1
                    var zone
                    if(x==1){
                        zone = table{{ table.id }}.getValue('B'+r)
                        if(zone == true){
                            let h2 = table{{ table.id }}.getValue('E'+r,true)
                            let h3 = table{{ table.id }}.getValue('H'+r,true)
                            let dynamic = ROUND(h2 - h3,2)
                            table{{ table.id }}.setValue('J'+r,dynamic);
                            {#减压阀分区的栓口动压和栓口静压的值相等#}
                            table{{ table.id }}.setValue('K'+r,dynamic);
                        }
                        else if(zone == false){
                            {#栓口动压#}
                            let h0 = table{{ table.id }}.getValue('C'+r,true)
                            let h3 = table{{ table.id }}.getValue('H'+r,true)
                            let h4 = table{{ table.id }}.getValue('I'+r,true)
                            let dynamic = ROUND(h0 - h3 - h4,2)
                            table{{ table.id }}.setValue('J'+r,dynamic);
                            {#栓口静压#}
                            let h1 = table{{ table.id }}.getValue('D'+r,true)
                            let height = table{{ table.id }}.getValue('G'+r,true)
                            let static = ROUND(h1 - height,2)
                            table{{ table.id }}.setValue('K'+r,static);
                        }
                    }
                    if(x==2 || x==3 || x==4 || x==6 || x==7 || x==8) {
                        zone = table{{ table.id }}.getValue('B'+r)
                        if(zone == false){
                            {#栓口动压#}
                            let h0 = table{{ table.id }}.getValue('C'+r,true)
                            let h3 = table{{ table.id }}.getValue('H'+r,true)
                            let h4 = table{{ table.id }}.getValue('I'+r,true)
                            let dynamic = ROUND(h0 - h3 - h4,2)
                            table{{ table.id }}.setValue('J'+r,dynamic);
                            {#栓口静压#}
                            let h1 = table{{ table.id }}.getValue('D'+r,true)
                            let height = table{{ table.id }}.getValue('G'+r,true)
                            let static = ROUND(h1 - height,2)
                            table{{ table.id }}.setValue('K'+r,static);
                        }
                        else if(zone == true){
                            let h2 = table{{ table.id }}.getValue('E'+r,true)
                            let h3 = table{{ table.id }}.getValue('H'+r,true)
                            let dynamic = ROUND(h2 - h3,2)
                            table{{ table.id }}.setValue('J'+r,dynamic);
                            {#减压阀分区的栓口动压和栓口静压的值相等#}
                            table{{ table.id }}.setValue('K'+r,dynamic);
                        }
                    }
                }
                {% elif table.id == 16 %}
                {#自动喷水系统“管网水头损失的计算-管道阻力”合计给“水泵扬程的计算-管网水头损失”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==10 && y==rows-1 && table17.setValue != undefined){
                        table17.setValue('D1',value);
                    }
                }
                {% elif table.id == 17 %}
                {#自动喷水系统“水泵扬程的计算-选泵扬程”给“自动喷水灭火系统供水泵选型-单台水泵扬程”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if(x==8 && table18.setValue != undefined){
                        table18.setValue('D1',value);
                    }
                    if((x==6 || x==8) && table19.setValue != undefined){
                        var p1=ROUND(table17.getValue('I1',true)*1.2,2);
                        var p2=ROUND(table17.getValue('G1',true),2);
                        var p = p1+p2;
                        p = ROUND(p,2)
                        table19.setValue('H1',p);
                    }
                }
                {% elif table.id == 19 %}
                {#自动喷水灭火系统“启泵压力计算-B、D”给“自动喷水灭火系统稳压泵计算-单台水泵扬程”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if((x==6 || x==8) && table20.setValue != undefined){
                        var p1=ROUND(table19.getValue('B1',true)*100,2);
                        var p2=ROUND(table19.getValue('D1',true),2);
                        var p = p1-p2+5;
                        p = ROUND(p,2)
                        table20.setValue('D1',p);
                    }
                }
                {% elif table.id == 22 %}
                {#“管网水头损失的计算-管道阻力”的值合计给“水泵扬程的计算-管网水头损失”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==9 && y==rows-1 && table23.setValue != undefined){
                        table23.setValue('D1',value);
                    }
                }
                {% elif table.id == 23 %}
                {#“水泵扬程的计算-选泵扬程”的值合计给“消防转输系统供水泵选型-单台水泵扬程”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==8 && table24.setValue != undefined){
                        table24.setValue('D1',value);
                    }
                }
                {% elif table.id == 25 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table25.rows.length
                    if(x==9 && y!=rows-1){
                        let row = parseInt(y)+1
                        var name = table25.getValue('A'+ row);
                        if(table26.getColumnData){
                            var arr = table26.getColumnData(0);
                            for(let i=0;i<arr.length;i++) {
                                if (arr[i] == name) {
                                    row = parseInt(i)+1
                                    table26.setValue('C'+ row,value);
                                }
                            }
                        }
                    }
                    if(x==9 && y==rows-1 && table28.setValue != undefined){
                        table28.setValue('D1',value);
                    }
                    {#用名称字段匹配值：“供水泵流量计算-系统设计流量”的值给“管网水头损失计算-总流量”#}
                    if(x==0){
                        var arr = table26.getColumnData(0)
                        for(let i=0;i<arr.length;i++){
                            if(arr[i]==value){
                                let rnum = parseInt(y)+1
                                var flow=table25.getValue('J'+rnum,true);
                                rnum = parseInt(i)+1
                                table26.setValue('C'+ rnum,flow);
                            }
                        }
                    }
                }
                {% elif table.id == 26 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    {#用合计值给下一张表赋值#}
                    if(x==10 && y==rows-1 && table27.setValue != undefined){
                        table27.setValue('D1',value);
                    }
                    {#用名称字段匹配值：“供水泵流量计算-系统设计流量”的值给“管网水头损失计算-总流量”#}
                    if(x==0){
                        var arr = table25.getColumnData(0)
                        for(let i=0;i<arr.length;i++){
                            if(arr[i]==value){
                                let rnum = parseInt(i)+1
                                var flow=table25.getValue('J'+rnum,true);
                                rnum = parseInt(y)+1
                                table26.setValue('C'+ rnum,flow);
                            }
                        }
                    }
                }
                {% elif table.id == 30 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    {#用名称字段匹配值：“七氟丙烷气体灭火系统计算-灭火设计用量或惰化设计用量”的值给“七氟丙烷气体灭火钢瓶选型表”#}
                    if(x==7){
                        let r1 = parseInt(y)+1
                        if(table34.getColumnData != undefined){
                            var arr = table34.getColumnData(0)
                            let name = table30.getValue('A'+ r1,true);
                            for(let i=0;i<arr.length;i++){
                                if(arr[i]==name){
                                    let rnum = parseInt(i)+1
                                    table34.setValue('C'+ rnum,value);
                                }
                            }
                        }
                    }
                }
                {% elif table.id == 31 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    {#用名称字段匹配值：“IG541气体灭火系统计算-灭火设计用量或惰化设计用量”的值给“IG541混合气体灭火钢瓶选型表”#}
                    if(x==7){
                        let r1 = parseInt(y)+1
                        if(table32.getColumnData != undefined){
                            var arr = table32.getColumnData(0)
                            let name = table31.getValue('A'+ r1,true);
                            for(let i=0;i<arr.length;i++){
                                if(arr[i]==name){
                                    let rnum = parseInt(i)+1
                                    table32.setValue('B'+ rnum,value);
                                }
                            }
                        }
                    }
                }
                {% elif table.id == 34 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let fc
                    {#用名称字段匹配值：“七氟丙烷气体灭火系统计算-灭火设计用量或惰化设计用量”的值给“七氟丙烷气体灭火钢瓶选型表”#}
                    if(x==0){
                        let r1 = parseInt(y)+1
                        var arr = table30.getColumnData(0)
                        for(let i=0;i<arr.length;i++){
                            if(arr[i]==value){
                                let rnum = parseInt(i)+1
                                let dose = table30.getValue('H'+ rnum,true);
                                rnum = parseInt(y)+1
                                table34.setValue('C'+ rnum,dose);
                            }
                        }
                    }
{#                    用名称字段匹配值：“钢瓶选型表-实际浓度”#}
                    if(x==8){
                        let r = parseInt(y)+1
                        let fa = table34.getValue('I'+ r,true);//T
                        let name = table34.getValue('A'+ r,true);
                        let fd = 0;
                        var arr = table30.getColumnData(0)
                        for(let i=0;i<arr.length;i++){
                            if(arr[i]==name){
                                let rnum = parseInt(i)+1
                                let pv = table30.getValue('C'+ rnum,true);//D
                                let cf = table30.getValue('F'+ rnum,true);//G
                                let bs = table30.getValue('E'+ rnum,true);//F
                                fd = 100*fa*bs/(pv*cf+fa*bs);
                                if (isNaN(fd)) {
                                    fd = ''
                                } else {
                                    fd = ROUND(fd,2);
                                }
                                table34.setValue('J'+ rnum,fd);
                            }
                        }
                    }
                }
                {% elif table.id == 35 %}
                {#灭火器配置#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==7  && table36.setValue != undefined){
                        table36.setValue('E1',value);
                    }
                }
                {% elif table.id == 36 %}
                {#“灭火器配置表-最小需配灭火级别”从“灭火级别配置表”获得#}
                sourcey = null
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if(x==0){
                        var arr = table35.getColumnData(0)
                        for(i=0;i<arr.length;i++){
                            if(arr[i]==value){
                                let rnum = parseInt(i)+1
                                var flow=table35.getValue('H'+rnum,true);
                                rnum = parseInt(y)+1
                                table36.setValue('E'+ rnum,flow);
                            }
                        }
                    }
                    if (x==7){// 下拉列表联动筛选操作
                        if (sourcey == null) {
                            sourcey=table36.options.columns[8].source
                        }
                        source = sourcey.concat()
                        var text = cell.innerText;
                        for(let i = source.length - 1; i >=0; i--) {
                            if (source[i]['group'] != text) {
                                source.splice(i,1)
                            }
                        }
                        table36.options.columns[8].source = source
                        var columnName = jexcel.getColumnNameFromId([parseInt(x) + 1, y]);
                        instance.jexcel.setValue(columnName, '');
                    }
                }
                {% elif table.id == 37 %}
                {#“防护冷却系统供水泵流量计算-系统设计流量”最大值给“防护冷却系统供水泵选型-单台水泵扬程”赋值#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    let rows = table{{ table.id }}.rows.length
                    if(x==8 && y==rows-1 && table40.setValue != undefined){
                        table40.setValue('D1',value);
                    }
                }
                {% elif table.id == 38 %}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    {#“管网水头损失的计算-管道阻力”合计给“水泵扬程的计算-管网水头损失”赋值#}
                    let rows = table{{ table.id }}.rows.length
                    if(x==10 && y==rows-1 && table39.setValue != undefined){
                        table39.setValue('D1',value);
                    }
                    {#“防护冷却系统供水泵流量计算-系统设计流量”给“管网水头损失的计算-总流量”赋值#}
                    if(x==0){
                        var arr = table37.getColumnData(0)
                        for(i=0;i<arr.length;i++){
                            if(arr[i]==value){
                                let rnum = parseInt(i)+1
                                var flow=table37.getValue('I'+rnum,true);
                                rnum = parseInt(y)+1
                                table38.setValue('C'+ rnum,flow);
                            }
                        }
                    }
                }
                {% elif table.id == 39 %}
                {#防护冷却系统启泵压力计算-泄压阀开启压力#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if((x==6 || x==10) && y==0 && table41.setValue != undefined){
                        var p=ROUND(table39.getValue('K1',true)*1.2+table39.getValue('G1',true),2);
                        table41.setValue('H1',p);
                    }
                }
                {% elif table.id == 41 %}
                {#防护冷却系统稳压泵计算-单台水泵扬程#}
                var changed{{ table.id }} = function(instance, cell, x, y, value) {
                    if((x==1 || x==3) && y==0 && table42.setValue != undefined){
                        var l=ROUND(table41.getValue('B1',true)*100-table41.getValue('D1',true),2);
                        table42.setValue('D1',l+5);
                    }
                }
                {% endif %}
                {#用下拉列表值给后一字段赋值#}
                var beforeChange{{ table.id }} = function(instance, cell, x, y, value) {
                    var pos=2
                    if ({{ table.id }} == 38 || {{ table.id }} == 16 || {{ table.id }} == 26)
                        pos=3
                    if (x == pos && {{ table.id }} != 1)
                        var title = $(cell).find('.jdropdown-selected').find('.jdropdown-title');
                        if (title!=undefined && title[0] != undefined) {
                            var desc = title[0].innerText;
                            var val = desc.substr(desc.lastIndexOf('计算内径') + 4);
                            var cellStr = jexcel.getColumnNameFromId([parseInt(x)+1, y]);
                            table{{ table.id }}.setValue(cellStr,val);
                        }
                }
            //更新合计行
            function collect{{ table.id }}() {
                 var tal = {{ table.tal|safe }}  //合计字段
                //更新合计公式
                if (tal.length > 0) {
                    len = table{{ table.id }}.rows.length - 1
                    for (i = 1; i < tal.length; i++) {
                        formula = '=' + tal[0] + '('
                        id = tal[i] + String(len + 1)
                        formula += tal[i] + 1 + ':' + tal[i] + String(len) + ')'
                        table{{ table.id }}.setValue(id, formula, true)
                    }
                }
            }
            function deleteRow{{ table.id }}() {
                table{{ table.id }}.formula = []
                collect{{ table.id }}()
            }
            table{{ table.id }} = jexcel(document.getElementById('table' + {{ table.id }}), {
                data: {{ table.data|safe }},
                columns: {{ table.columns|safe }},
                nestedHeaders: {{ table.nested_header|safe }},
                wordWrap: true,
                allowComments: true,
                allowManualInsertRow:false,// 禁用回车创建行
            {% if table.id == 25 or table.id == 26 or table.id == 3 or table.id == 4 or table.id == 6 or table.id == 8 or table.id == 10 or table.id == 11 or table.id == 13 or table.id == 15 or table.id == 16 or table.id == 17 or table.id == 19 or table.id == 21 or table.id == 22 or table.id == 23 or table.id == 29 or table.id == 30 or table.id == 31 or table.id == 34 or table.id == 35 or table.id == 36 or table.id == 37 or table.id == 38 or table.id == 39 or table.id == 41%}
                onchange:changed{{ table.id }},
            {% endif %}
                onbeforechange: beforeChange{{ table.id }},
            {% if table.id == 29 %}
                onbeforedeleterow: beforeDelete,
            {% endif %}
                ondeleterow: deleteRow{{ table.id }},
                contextMenu: function (obj, x, y, e) {
                    var items = [];
                    if (y != null) {
                        // Insert new row
                        {#if (obj.options.allowInsertRow == true) {#}
                        {#    items.push({#}
                        {#        title: obj.options.text.insertANewRowAfter,#}
                        {#        onclick: function () {#}
                        {#            obj.insertRow(1, parseInt(y));#}
                        {#        }#}
                        {#    });#}
                        {#}#}
                        if (obj.options.allowDeleteRow == true) {
                            items.push({
                                title: obj.options.text.deleteSelectedRows,
                                onclick: function () {
                                    tal = {{ table.tal|safe }}
                                    if (tal.length > 0) {
                                        len = 2
                                    } else {
                                        len = 1
                                    }
                                    if (table{{ table.id }}.rows.length == len) {
                                        addrow{{ table.id }}()
                                    }
                                    obj.deleteRow(obj.getSelectedRows().length ? undefined : parseInt(y));
                                }
                            });
                        }
                    }
                    return items;
                },
                text: {
                    {#insertANewRowAfter: "插入一行",#}
                    deleteSelectedRows: "删除选中行"
                },
                //设置合计行的readonly，删除dropdown和checkbox
                updateTable: function(el, cell, x, y, source, value, id) {
                    h = this.data.length - 1
                    tal = {{ table.tal|safe }}  //合计字段
                    if (y == h && tal.length > 0) {
                        cell.classList.remove('dropdown');
                        if(cell.children.length==1)
                            var child = $(cell).find("input");
                            if(child!=undefined && (source == '合计' || source == '最大值'))
                                child.remove();
                            var checkbox = $(cell).find("input:checkbox")
                            if (checkbox!=undefined)
                                checkbox.attr("onclick", "return false");
                        if (source == '合计' || source == '最大值') {
                            cell.innerHTML = source
                        }
                        cell.classList.add('readonly');
                    }
                }
            });

            //在合计行上方添加空行，只有一个合计行时减2
            function addrow{{ table.id }}() {
                var com = {{ table.com|safe }} //提示字段
                var fo = {{ table.fo|safe }} //公式字段
                var de = {{ table.de|safe }} //默认值字段
                var tal = {{ table.tal|safe }}  //合计字段
                var len = table{{ table.id }}.rows.length
                if (tal.length == 0) {//没有合计字段插入到最后
                    table{{ table.id }}.insertRow(1, len, true)
                    len++
                } else {
                    table{{ table.id }}.insertRow(1, len - 1, true)
                }
                if({{ table.id }}== 22 ){
                    var flow=document.getElementById('t_flow');
                    var v = flow.value
                    if(table22.setValue != undefined){
                        table22.setValue('B'+len,v);
                    }
                }
                //添加提示信息
                for (i = 0; i < com.length; i++) {
                    table{{ table.id }}.setComments(com[i][0] + String(len), com[i][1])
                }

                //添加公式
                for (i = 0; i < fo.length; i++) {
                    var id = fo[i][0] + String(len)
                    var formula = '=' + fo[i][1].replace(/[A-Z]+/g, function (a) {
                                    if (a.length > 1) {
                                        return a
                                    } else {
                                        return a + String(len)
                                    }
                                })
                    table{{ table.id }}.setValue(id, formula)
                }
                //添加默认值
                for (i = 0; i < de.length; i++) {
                    var id = de[i][0] + String(l)
                    var cellValue = table{{ table.id }}.getValue(id)
                    if (cellValue === '') {
                        var formula = '=' + de[i][1].replace(/[A-Z]+/g, function (a) {
                                    if (a.length > 1) {
                                        return a
                                    } else {
                                        return a + String(l)
                                    }
                                })
                        table{{ table.id }}.setValue(id, formula)
                    }

                }
                //更新合计公式
                collect{{ table.id }}()
            }
            //初始化表格，添加注释，添加公式，添加合计
            function initRows{{ table.id }}() {
                var com = {{ table.com|safe }} //提示字段
                var fo = {{ table.fo|safe }} //公式字段
                var de = {{ table.de|safe }} //默认值字段
                var tal = {{ table.tal|safe }}  //合计字段
                var len = table{{ table.id }}.rows.length
                if (tal.length > 0) {//去掉最后一行
                    len--;
                }
                if (len == 0) {
                    table{{ table.id }}.insertRow(1, len, true)
                    len++
                }
                for (l = 1; l <= len; l++) {
                    //添加注释
                    for (i = 0; i < com.length; i++) {
                        table{{ table.id }}.setComments(com[i][0] + String(l), com[i][1])
                    }
                    //添加公式
                    for (i = 0; i < fo.length; i++) {
                        var id = fo[i][0] + String(l)
                        var formula = '=' + fo[i][1].replace(/[A-Z]+/g, function (a) {
                                    if (a.length > 1) {
                                        return a
                                    } else {
                                        return a + String(l)
                                    }
                                })
                        table{{ table.id }}.setValue(id, formula)
                    }
                    //添加默认值
                    for (i = 0; i < de.length; i++) {
                        var id = de[i][0] + String(l)
                        var cellValue = table{{ table.id }}.getValue(id)
                        if (cellValue === '') {
                            var formula = '=' + de[i][1].replace(/[A-Z]+/g, function (a) {
                                        if (a.length > 1) {
                                            return a
                                        } else {
                                            return a + String(l)
                                        }
                                    })
                            table{{ table.id }}.setValue(id, formula)
                        }

                    }
                }
                collect{{ table.id }}()
            }
            initRows{{ table.id }}();
            {% if table.id == 29 %}
                columnData = table29.getColumnData(0)
                arr = {'196': [30, 34], '197': [31, 32], '198': [33]} // 系统名称对应的表id

                for(i=0; i<columnData.length;i++){
                    let row = i + 1;
                    olda[row] = {}
                    olda[row]['A'] = table29.getValue('A'+ row);
                    olda[row]['B'] = table29.getValue('B'+ row);
                    if (olda[row]['A'] != '') {
                        olda[row]['ids'] = arr[olda[row]['A']] //对应的表
                        olda[row]['row'] = rows[olda[row]['A']]++ //对应的表中的行
                    }
                }

                var newRows = function (row) {
                    ids = arr[olda[row]['A']]
                    nrow = rows[olda[row]['A']]
                    for (k = 0; k < ids.length; k++) {
                        let id = ids[k]
                        if (nrow != 1) {
                            addrow = 'addrow' + id + '()'
                            eval(addrow)
                        }
                        let setValue = 'table' + id + ".setValue('A"+ nrow +"','" + olda[row]['B'] + "')"
                        eval(setValue)
                    }
                    olda[row]['ids'] = ids //对应的表
                    olda[row]['row'] = rows[olda[row]['A']]++ //对应的表中的行
                }
                var changeRows = function (row) {
                    ids = arr[olda[row]['A']]
                    nrow = olda[row]['row']
                    for (k = 0; k < ids.length; k++) {
                        id = ids[k]
                        setValue = 'table' + id + ".setValue('A"+ nrow +"','" + olda[row]['B'] + "')"
                        eval(setValue)
                    }
                }
                var deleteRows = function (row) { // 删除对应行
                    ids = olda[row]['ids']
                    nrow = olda[row]['row']
                    for (k = 0; k < ids.length; k++) {
                        id = ids[k]
                        getLen = 'table' + id + '.rows.length'
                        len = eval(getLen)
                        if (len == 1) { //如果只有一行设置成空
                            setValue = 'table' + id + ".setValue('A"+ nrow +"','')"
                            eval(setValue)
                        } else {
                            getValue = 'table' + id + ".getValue('A"+ nrow + "')"
                            value = eval(getValue)
                            if (value == olda[row]['B']) {
                                tmpRow = nrow - 1
                                setValue = 'table' + id + ".deleteRow("+ tmpRow + ")"
                                eval(setValue)
                            }
                        }
                    }
                    for (k = 1; k < olda.length; k++) {//从1开始
                        idss = olda[k]['ids']
                        if (idss == ids && olda[k]['row'] > nrow) {
                            olda[k]['row']--
                        }
                    }
                }

                function  checkUnique() {
                    for(let i=1;i<olda.length-1;i++){
                            for(let j=i+1;j<olda.length;j++){
                                if(olda[i]['A']==olda[j]['A'] && olda[i]['B']==olda[j]['B'] && olda[i]['A']!='' && olda[i]['B']!='')
                                    $.globalMessenger().post({
                                        message: '“系统名称”、“防护区名称”组合的值应该唯一！',
                                        hideAfter: 6,
                                        type: 'error',
                                    });
                            }
                        }
                }

            {% endif %}
        {% endfor %}
{#        window.onbeforeunload = function(event) {#}
{#            event.returnValue = "有数据未保存";#}
{#        };#}
    </script>
{% endblock %}